{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=['alice','david','carolina']\n",
    "for b in a:\n",
    "    print(b)\n",
    "\n",
    "for b in a:\n",
    "    print(b.title()+', that was a great trick!')\n",
    "    print(\"I can't wait to see your next trick, \"+b.title()+\".\\n\")\n",
    "\n",
    "print(\"Thank you, everyone. That was a great magic show! \")\n",
    "\n",
    "#创建数字列表\n",
    "for value in range(1,5):\n",
    "    print(value)\n",
    "\n",
    "for value in range(1,6):\n",
    "    print(value)\n",
    "\n",
    "numbers=list(range(1,6))\n",
    "print(numbers)\n",
    "#指定步长，只打印偶数\n",
    "even_numbers=list(range(2,11,2))\n",
    "print(even_numbers)\n",
    "\n",
    "#前十个整数的平方\n",
    "squares=[]\n",
    "for value in range(1,11):\n",
    "    square=value**2\n",
    "    squares.append(square)\n",
    "\n",
    "print(squares)\n",
    "#\n",
    "squares=[]\n",
    "for value in range(1,11):\n",
    "    squares.append(value**2)\n",
    "print(squares)\n",
    "#\n",
    "digits=[1,2,3,4,5,6,7,8,9,0]\n",
    "print(min(digits))\n",
    "print(max(digits))\n",
    "print(sum(digits))\n",
    "\n",
    "#列表解析，直接上\n",
    "squares=[value**2 for value in range(1,11)]\n",
    "print(squares)\n",
    "\n",
    "#切片\n",
    "print(squares[0:3])\n",
    "print(squares[1:4])\n",
    "print(squares[:4])\n",
    "print(squares[2:])\n",
    "print(squares[-3:])\n",
    "#\n",
    "#遍历，也就是循环列表中的一部分\n",
    "players = ['charles','martina','michael','florence','eli']\n",
    "print('Here are the first three players on my team: ')\n",
    "for player in players[:3]:\n",
    "    print(player.title())\n",
    "\n",
    "#复制列表，其实就是切片并将值赋给新的列表，原列表不变\n",
    "my_foods=['pizza','falafel','carrot cake']\n",
    "friend_foods=my_foods[:]\n",
    "\n",
    "my_foods.append('cannoli')\n",
    "friend_foods.append('ice cream')\n",
    "\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "\n",
    "print(\"\\nMy friend's favorite foods are:\")\n",
    "print(friend_foods)\n",
    "\n",
    "#直接a列表=b列表，ab相关联，a变b变，b变a变\n",
    "\n",
    "a=[0,1,2,3]\n",
    "b=a\n",
    "a.append(4)\n",
    "b.append(5)\n",
    "print(a)\n",
    "print(b)\n",
    "\n",
    "#元组，不可改变的列表\n",
    "dimensions=(200,500,1)\n",
    "print(dimensions)\n",
    "print(dimensions[1])\n",
    "print(dimensions[0])\n",
    "\n",
    "for dimension in dimensions:\n",
    "    print(dimension)\n",
    "\n",
    "dimensions=(10,20,30)\n",
    "print(\"\\nModified dimensions:\")\n",
    "for dimension in dimensions:\n",
    "    print(dimension)\n",
    "\n",
    " #PEP 8代码格式规范\n",
    "\n",
    "\n",
    "\n",
    "# 0402 第四章的习题前面很简单，最后两道有点难\n",
    "# 4-1到4-13都很简单，放到第一部分\n",
    "# 4-14 是要求阅读PEP 8，4-15 缩进，行宽，空行\n",
    "\n",
    "\n",
    "# 习题 4-1\n",
    "\n",
    "pizzas=['榴莲芝士披萨','海鲜至尊披萨','黑椒牛肉披萨']\n",
    "\n",
    "for pizza in pizzas:\n",
    "    print(pizza)\n",
    "for pizza in pizzas:\n",
    "    print(pizza+\"超好吃\")\n",
    "print(\"我是真喜欢必胜客，尤其是白宇哥哥代言之后。\")\n",
    "\n",
    "# 习题 4-2\n",
    "creatures=['我','你','他']\n",
    "for creature in creatures:\n",
    "    print(creature)\n",
    "\n",
    "for creature in creatures:\n",
    "    print(creature+'爱吃披萨！')\n",
    "\n",
    "print('大家都爱吃披萨！')\n",
    "\n",
    "# 习题 4-3\n",
    "\n",
    "for n in range(1,21):\n",
    "    print(n)\n",
    "\n",
    "# 习题 4-4\n",
    "\n",
    "# for n in range(1,1000001):\n",
    "#     print(n)\n",
    "\n",
    "# 习题 4-5\n",
    "a=range(1,1000001)\n",
    "\n",
    "print(min(a))\n",
    "print(max(a))\n",
    "print(sum(a))\n",
    "\n",
    "# 习题 4-6\n",
    "\n",
    "for odd_number in range(1,20,2):\n",
    "    print(odd_number)\n",
    "\n",
    "\n",
    "# 习题4-7 3的倍数，步长为3\n",
    "\n",
    "for multiple_of_three in range(3,31,3):\n",
    "    print(multiple_of_three)\n",
    "\n",
    "# 习题 4-8 立方\n",
    "\n",
    "b=[]\n",
    "a=range(1,11)\n",
    "for n in a:\n",
    "    b.append(n**3)\n",
    "\n",
    "print(b)\n",
    "\n",
    "# 习题 4-9 立方解析\n",
    "\n",
    "cubic=[n**3 for n in range(1,11)]\n",
    "print(cubic)\n",
    "\n",
    "# 4-10 切片\n",
    "\n",
    "a=range(1,11)\n",
    "\n",
    "print(\"\\nThe first three items in the list are:\\n\")\n",
    "\n",
    "# print(a[:3])\n",
    "\n",
    "for n in a[0:3]:\n",
    "    print(n)\n",
    "\n",
    "print(\"\\nThe items from the middle of the list are:\\n\")\n",
    "\n",
    "# print(a[3:6])\n",
    "\n",
    "for n in a[3:6]:\n",
    "    print(n)\n",
    "\n",
    "print(\"\\nThe last three items in the list are:\\n\")\n",
    "\n",
    "# print(a[-3:])\n",
    "\n",
    "for n in a[-3:-1]: #要想去最后一个元素，只能用空，如果用-1，不包含10\n",
    "    print(n)\n",
    "\n",
    "\n",
    "# 4-11\n",
    "pizzas=['榴莲芝士披萨','海鲜至尊披萨','黑椒牛肉披萨']\n",
    "pizzas_2=pizzas[:]\n",
    "\n",
    "pizzas.append(\"巧克力奶茶披萨\")\n",
    "pizzas_2.append(\"土豆山药披萨\")\n",
    "print(\"\\nMy favorite pizzas are:\\n\")\n",
    "print(pizzas)\n",
    "print(\"\\nMy friend's favorite pizzas are:\\n\")\n",
    "print(pizzas_2)\n",
    "\n",
    "# 4-12\n",
    "\n",
    "my_foods=['pizza','falafel','carrot cake']\n",
    "friend_foods=my_foods[:]\n",
    "\n",
    "my_foods.append('cannoli')\n",
    "friend_foods.append('ice cream')\n",
    "\n",
    "print(\"My favorite foods are:\")\n",
    "# print(my_foods)\n",
    "\n",
    "for food in my_foods:\n",
    "    print(food)\n",
    "\n",
    "print(\"\\nMy friend's favorite foods are:\")\n",
    "# print(friend_foods)\n",
    "for food in friend_foods:\n",
    "    print(food)\n",
    "\n",
    "# 3-13\n",
    "buffet=('烤肉','冰淇淋','炸鸡','饮料','灌汤包')\n",
    "for food in buffet:\n",
    "    print(food)\n",
    "\n",
    "# buffet(0)='水果'\n",
    "#can't assign to function call 无法分配给函数调用\n",
    "\n",
    "buffet=('烤肉','水果','沙拉','饮料','灌汤包')\n",
    "for food in buffet:\n",
    "    print(food)\n",
    "#\n",
    "# # 4-14 PEP8  https://www.python.org/dev/peps/pep-0008/ 解读\n",
    "#\n",
    "# # 4-15\n",
    "# # PEP 8 是一个编程必须遵守的准则，其中要求每行长度不超过80个字符，我这里用的是Pycharm，所以重点看一下载Pycharm中的设置，https://blog.csdn.net/weixin_42122355/article/details/83373889\n",
    "# # 函数之间或类的方法之间用空行分隔，表示一段新的代码的开始。类和函数入口之间也用一行空行分隔，以突出函数入口的开始。\n",
    "# # 空行与代码缩进不同，空行并不是Python语法的一部分。书写时不插入空行，Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码，便于日后代码的维护或重构。\n",
    "# # 记住：空行也是程序代码的一部分。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
